package com.td.module.farmer.controller.app.employ;

import com.td.framework.common.pojo.CommonResult;
import com.td.framework.common.pojo.PageResult;
import com.td.framework.excel.core.util.ExcelUtils;
import com.td.framework.operatelog.core.annotations.OperateLog;
import com.td.module.farmer.controller.admin.employ.vo.*;
import com.td.module.farmer.convert.employ.EmployConvert;
import com.td.module.farmer.dal.dataobject.employ.EmployDO;
import com.td.module.farmer.service.employ.EmployService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.td.framework.common.pojo.CommonResult.success;
import static com.td.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "app - 农业社会化服务")
@RestController
@RequestMapping("/farmer/employ")
@Validated
public class EmployAppController {

    @Resource
    private EmployService employService;

    @PostMapping("/create")
    @ApiOperation("创建农业社会化服务")
    public CommonResult<Long> createEmploy(@Valid @RequestBody EmployCreateReqVO createReqVO) {
        return success(employService.createEmploy(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新农业社会化服务")
    public CommonResult<Boolean> updateEmploy(@Valid @RequestBody EmployUpdateReqVO updateReqVO) {
        employService.updateEmploy(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除农业社会化服务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteEmploy(@RequestParam("id") Long id) {
        employService.deleteEmploy(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得农业社会化服务")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<EmployRespVO> getEmploy(@RequestParam("id") Long id) {
        EmployDO employ = employService.getEmploy(id);
        return success(EmployConvert.INSTANCE.convert(employ));
    }

    @GetMapping("/list")
    @ApiOperation("获得农业社会化服务列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<EmployRespVO>> getEmployList(@RequestParam("ids") Collection<Long> ids) {
        List<EmployDO> list = employService.getEmployList(ids);
        return success(EmployConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得农业社会化服务分页")
    public CommonResult<PageResult<EmployRespVO>> getEmployPage(@Valid EmployPageReqVO pageVO) {
        PageResult<EmployDO> pageResult = employService.getEmployPage(pageVO);
        return success(EmployConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出农业社会化服务 Excel")
    @OperateLog(type = EXPORT)
    public void exportEmployExcel(@Valid EmployExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<EmployDO> list = employService.getEmployList(exportReqVO);
        // 导出 Excel
        List<EmployExcelVO> datas = EmployConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "农业社会化服务.xls", "数据", EmployExcelVO.class, datas);
    }

}
